#include "subsetdao.h"
#include "dbutil.h"
#include <QMap>
#ifdef LINUX_DB
#include <stdio.h>
#else
#include <QSqlQuery>
#include <QVariant>
#include <QSqlError>
#include <QSqlDatabase>
#endif
#include <iostream>
using namespace std;
SubSetDao::SubSetDao()
{
}
bool SubSetDao::updateSubset(SubSet ss)
{
    QString esql = "update JYDW_SUBSET set px=%1,py=%2 where SID=%3";
    esql = esql.arg(DbUtils::getStringFormInt(ss.getX()), DbUtils::getStringFormInt(ss.getY()),
                    DbUtils::getStringFormInt(ss.getSid()));
#ifdef LINUX_DB
    char sql[500];
    DbUtils dbUtils;
    sprintf(sql, esql.toLocal8Bit().data());
    int result = dbUtils.exec(sql);
    dbUtils.freeRes();
    if (result == 0)
    {
        return true;
    }
    else
    {
        DbUtils::ErrorLog("SubSetDao::updateSubset", "SQL:" + esql);
        return false;
    }
#else
    QSqlQuery query;
    query.prepare(esql);
    if (!query.exec())
    {
        DbUtils::ErrorLog("SubSetDao::updateSubset", "SQL:" + esql);
        return false;
    }
    return true;
#endif
}

list<SubSet *> SubSetDao::getSubSet()
{
    QString str("select  *  from JYDW_SUBSET  where subset_ip not in (select subset1 from jydw_bi_subset_custom) and subset_ip not in (select subset2 from jydw_bi_subset_custom) order by sid");
    QMap<QString, int> qmap;
#ifdef LINUX_DB
    list<SubSet *> subList;
    char sql[500];
    DbUtils dbUtils;
    sprintf(sql, str.toLocal8Bit().data());
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct Subset_result), "SubSetDao::getSubSet");
    while (result != NULL)
    {
        Subset_result *sub_result = (Subset_result *)result;
        SubSet *subset = new SubSet;
        subset->setSubSetIp(sub_result->subset_ip);
        subset->setSubSetmask(sub_result->subset_mask);
        if (!qmap.contains(subset->getSubSetIp() + "," + subset->getSubSetmask()))
        {
            subset->setSid(sub_result->sid);
            subset->setDeviceId(sub_result->device_id);
            subset->setConnectType(sub_result->connType);
            subset->setX(sub_result->px);
            subset->setY(sub_result->py);
            subList.push_back(subset);
            qmap.insert(subset->getSubSetIp() + "," + subset->getSubSetmask(), subset->getSid());
        }
        result = dbUtils.getNextRow();
    }
    dbUtils.freeRes();
    return subList;
#else
    list<SubSet *> result;
    if (DbUtils::isDbOpen())
    {
        QSqlQuery query;
        query.prepare(str);
        if (!query.exec())
        {
            DbUtils::ErrorLog("SubSetDao::getSubSet", "SQL:" + str);
        }
        else
        {
            while (query.next())
            {
                SubSet *device = new SubSet;
                device->setSubSetIp(query.value(3).toString());
                device->setSubSetmask(query.value(4).toString());
                if (!qmap.contains(device->getSubSetIp() + "," + device->getSubSetmask()))
                {
                    device->setSid(query.value(0).toInt());
                    device->setDeviceId(query.value(1).toInt());
                    device->setConnectType(query.value(5).toInt());
                    device->setX(query.value(8).toInt());
                    device->setY(query.value(9).toInt());
                    result.push_back(device);
                    qmap.insert(device->getSubSetIp() + "," + device->getSubSetmask(), device->getSid());
                }
            }
        }
    }

    return result;
#endif
}
SubSet *SubSetDao::getSubSetByIp(QString ip)
{
    SubSet *subset = new SubSet;
    QString str = "select sid,device_id,subset_ip,subset_mask,connect_type from JYDW_SUBSET where SUBSET_IP='%1' ";
    str = str.arg(ip);
#ifdef LINUX_DB
    struct Subset_result1
    {
        int sid;
        int device_id;
        char subset_ip[16];
        char subset_mask[16];
    } ;
    char sql[500];
    DbUtils dbUtils;
    sprintf(sql, str.toLocal8Bit().data());
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct Subset_result1), "SubSetDao::getSubSetByIp");
    while (result != NULL)
    {
        Subset_result1 *sub_result = (Subset_result1 *)result;
        subset->setSid(sub_result->sid);
        subset->setDeviceId(sub_result->device_id);
        subset->setSubSetIp(sub_result->subset_ip);
        subset->setSubSetmask(sub_result->subset_mask);
        break;
    }
    dbUtils.freeRes();
#else
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("SubSetDao::getSubSetByIp", "SQL:" + str);
    }
    while (query.next())
    {
        subset->setSid(query.value(0).toInt());
        subset->setDeviceId(query.value(1).toInt());
        subset->setSubSetIp(query.value(2).toString());
        subset->setSubSetmask(query.value(3).toString());
        subset->setConnectType(query.value(4).toInt());
        break;
    }
    query.clear();
#endif
    return subset;
}

list<SubSet *> SubSetDao::getSubSetByDeviceId(int deviceId)
{
    QString str = "select * from JYDW_SUBSET where DEVICE_ID=%1 and CONNECT_TYPE!=2";
    str = str.arg(deviceId);
#ifdef LINUX_DB
    list<SubSet *> subList;
    char sql[500];
    DbUtils dbUtils;
    sprintf(sql, str.toLocal8Bit().data());
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct Subset_result), "SubSetDao::getSubSetByDeviceId");
    while (result != NULL)
    {
        Subset_result *sub_result = (Subset_result *)result;
        SubSet *subset = new SubSet;
        subset->setSid(sub_result->sid);
        subset->setDeviceId(sub_result->device_id);
        subset->setSubSetIp(sub_result->subset_ip);
        subset->setSubSetmask(sub_result->subset_mask);
        subset->setConnectType(sub_result->connType);
        subset->setX(sub_result->px);
        subset->setY(sub_result->py);
        subList.push_back(subset);
        result = dbUtils.getNextRow();
    }
    dbUtils.freeRes();
    return subList;
#else
    list<SubSet *> result;
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("SubSetDao::getSubSetByDeviceId", "SQL:" + str);
    }
    while (query.next())
    {
        SubSet *device = new SubSet;
        device->setSid(query.value(0).toInt());
        device->setDeviceId(query.value(1).toInt());
        device->setSubSetIp(query.value(3).toString());
        device->setSubSetmask(query.value(4).toString());
        device->setConnectType(query.value(5).toInt());
        device->setX(query.value(8).toInt());
        device->setY(query.value(9).toInt());
        result.push_back(device);
    }
    query.clear();
    return result;
#endif
}
